package com.cfpamf.ms.insur.pay.dao;

import com.cfpamf.ms.insur.pay.pojo.po.BaseNoUserPO;
import com.cfpamf.ms.insur.pay.pojo.po.BasePO;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.common.IdsMapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * Create By zhengjing on 2019-09-25 10:16
 */
public interface CommonMapper<T> extends Mapper<T>, MySqlMapper<T>, IdsMapper<T> {

    default int insertInit(T t) {
        if (t instanceof BasePO) {
            if (Objects.isNull(((BasePO) t).getEnabledFlag()))
                ((BasePO) t).setEnabledFlag(Boolean.TRUE);
        }
        return insert(t);
    }

    default int insertListInit(List<T> models) {
        if (CollectionUtils.isEmpty(models)) return 0;
        for (T model : models) {
            if (model instanceof BaseNoUserPO) {
                BaseNoUserPO tmp = (BaseNoUserPO) model;
                if (Objects.isNull(tmp.getEnabledFlag()))
                    tmp.setEnabledFlag(Boolean.TRUE);
                if (Objects.isNull(tmp.getCreateTime()))
                    tmp.setCreateTime(LocalDateTime.now());

                if (Objects.isNull(tmp.getUpdateTime()))
                    tmp.setUpdateTime(LocalDateTime.now());
            }
        }
        return insertList(models);
    }
}
